package com.fau.wm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalInventoryDetail;
import com.fau.wm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 盘点详细Action
 * @author kangyj
 * @datetime 2014年8月16日,下午3:12:12
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class InventoryDetailAction extends PublicAction implements ModelDriven<MedicalInventoryDetail> {

	private static final long serialVersionUID = -1614962839032818100L;
	
	private MedicalInventoryDetail inventoryDetail = new MedicalInventoryDetail();
	
	/**
	 * @desc 盘点详细页面初始化
	 * @return String
	 * @author kangyj
	 * @datetime 2014年8月16日,下午3:11:40
	 */
	@Action(value = "inventory_detail_init", results = {@Result(name = SUCCESS, location = "inventory_detail_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 导入库存盘点
	 * @return String
	 * @author liy
	 * @datetime 2014年12月23日,下午8:20:58
	 */
	@Action(value = "inventory_import", results = {@Result(name = SUCCESS, location = "inventory_import.jsp")})
	public String inventoryImport(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询盘点详细
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月16日,下午3:11:20
	 */
	@Action(value = "inventory_detail_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getInventoryManagerService().findInventoryDetailList(inventoryDetail.getInventoryId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询药品历史盘点记录
	 * @return null
	 * @author liy
	 * @datetime 2014年12月23日,上午9:57:47
	 */
	@Action(value = "inventory_history_view")
	public String historyView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ActionContext.getContext().getParameters();
		pageBean = this.baseAllService.getInventoryManagerService().findMedicineHistoryInventory(map,inventoryDetail.getMedicineId());
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 添加盘点详细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月16日,下午3:13:45
	 */
	@Action(value = "inventory_detail_add", results = {@Result(name = SUCCESS, location = "inventory_detail_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存盘点详细
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月16日,下午3:09:47
	 */
	@Action(value = "inventory_detail_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getInventoryManagerService().saveInventoryDetail(inventoryDetail);
		if(flag){
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 修改盘点详细
	 * @return String
	 * @author kangyj
	 * @datetime 2014年8月16日,下午3:09:35
	 */
	@Action(value = "inventory_detail_edit", results = {@Result(name = SUCCESS, location = "inventory_detail_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存修改盘点详细
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月16日,下午3:09:19
	 */
	@Action(value = "inventory_detail_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		
		if(StringUtils.isNotBlank(inventoryDetail.getId())){
			this.baseAllService.getInventoryManagerService().saveEditInventoryDetail(inventoryDetail);
			map.put("success", true);
		}else {
			map.put("result", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除盘点详细
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月16日,下午3:09:05
	 */
	@Action(value = "inventory_detail_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(inventoryDetail.getId())){
			this.baseAllService.getInventoryManagerService().delInventoryDetail(inventoryDetail);
			map.put("success", true);
		}else {
			map.put("result", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询药品
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月16日,下午4:28:08
	 */
	@Action(value = "inventory_detail_find_medicine")
	public String findMedicineByName(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String key = request.getParameter("key");
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getMedicineService().findMedicineByName(admin, key);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询将要添加的药品是否已存在
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月16日,下午5:03:32
	 */
	@Action(value = "inventory_detail_check_medicine")
	public String checkMedicineExist(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		Boolean flag = this.baseAllService.getInventoryManagerService().checkMedicineExist(inventoryDetail.getInventoryId(), inventoryDetail.getMedicineId());
		if(flag) {
			map.put("able", true);
		} else {
			map.put("able", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 导出当前仓库的盘点表
	 * @return null
	 * @author liy
	 * @datetime 2014年12月23日,下午5:49:43
	 */
	@Action(value = "inventory_export")
	public String exportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"库存盘点.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getInventoryManagerService().exportExcelAll(admin, inventoryDetail.getWarehouseId());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导入盘点excel
	 * @return null
	 * @author liy
	 * @datetime 2014年12月23日,下午7:57:18
	 */
	@Action(value="import_inventory")
	public String importInventory(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			String info = this.baseAllService.getInventoryManagerService().importInventory(admin,inventoryDetail.getInventoryId(), file);
			if(StringUtils.isBlank(info)){
				map.put("success", true);
				map.put("message", "导入成功！");
				formatJSON(response, map);
			}else{
				map.put("success", false);
				map.put("message", info);
				formatJSON(response, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 验证所选药品在仓库中是否存在
	 * @return
	 * @author kangyj
	 * @datetime 2015年3月11日,下午12:11:23
	 */
	@Action(value="inventory_valid_pro_exist")
	public String validProExist() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getInventoryManagerService().validProExist(inventoryDetail);
		map.put("success", flag);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 完成盘点时验证产品是否存在
	 * @return
	 * @author kangyj
	 * @datetime 2015年3月11日,下午2:03:01
	 */
	@Action(value="inventory_complete_valid_pro")
	public String inventoryValidPro() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		String errorInfo = this.baseAllService.getInventoryManagerService().inventoryValidPro(inventoryDetail);
		map.put("errorInfo", errorInfo);
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalInventoryDetail getModel() {
		return inventoryDetail;
	}

}
